﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

namespace IYAKASAPortal.InfoEnter
{
    public partial class Vehicle_Infor_Update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindHPZL();
                if(Request["UniqueKey"]!= null && Request["UniqueKey"].ToString()!="")
                {
                    string strID = Request["UniqueKey"].ToString();
                    GetInformation(strID);
                }
            }
        }

        private void BindHPZL()
        {
            CONST_DEFINE_BLL bll = new CONST_DEFINE_BLL();
            ListItem item;
            var hpzl = bll.GetModelList("CON_CHNAME='HPZL' and CON_CODE<>15 order by CON_ORDER asc");
            foreach (var v in hpzl)
            {
                item = new ListItem();
                item.Text = v.CON_CON;
                item.Value = v.CON_CODE;
                drpHPZL.Items.Add(item);
            }
        }

        private void GetInformation(string strID)
        {
            RESULT_VEHICLE_INFO_BLL bll = new RESULT_VEHICLE_INFO_BLL();
            var vInfo = bll.GetModelList("ID='" + strID + "'");

            if (vInfo.Length > 0)
            {
                txtHPHMStart.Text = vInfo[0].HPHM.Substring(0, 1);
                txtHPHM.Text = vInfo[0].HPHM.Substring(1, vInfo[0].HPHM.Length - 1);
                drpHPZL.SelectedValue = vInfo[0].HPZLDH;
                txtOwner.Text = vInfo[0].SYR;
                txtAddress.Text = vInfo[0].LXDZ;
                txtPhone.Text = vInfo[0].LXDH;
                txtDLRDH.Text = vInfo[0].DLRLXDH;
                txtDLRMC.Text = vInfo[0].DLRMC;
                txtYZBH.Text = vInfo[0].YZBH;
                txtJCLSH.Text = vInfo[0].JCLSH;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strJCLSH = txtJCLSH.Text.Trim();
            string strLXDZ = txtAddress.Text.Trim();
            string strLXDH = txtPhone.Text.Trim();
            string strYZBH = txtYZBH.Text.Trim();
            string strDLRDH = txtDLRDH.Text.Trim();
            string strDLRMC = txtDLRMC.Text.Trim();

            RESULT_VEHICLE_INFO info = new RESULT_VEHICLE_INFO();
            RESULT_VEHICLE_INFO_BLL bll = new RESULT_VEHICLE_INFO_BLL();

            info = bll.GetModel(strJCLSH);

            if (string.IsNullOrEmpty(info.JCLSH) == false)
            {
                info.LXDZ = strLXDZ;
                info.LXDH = strLXDH;
                info.YZBH = strYZBH;
                info.DLRLXDH = strDLRDH;
                info.DLRMC = strDLRMC;

                bool b = bll.Update(info,info.JCLSH);

                if (b)
                {
                    gfLibrary.gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "保存成功！");
                    return;
                }
                else
                {
                    gfLibrary.gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "保存失败！");
                    return;
                }

            }

        }

        private void PrintPZSQB()
        {
            if (Request["UniqueKey"] != null && Request["UniqueKey"].ToString() != "")
            {
                string strID = Request["UniqueKey"].ToString();
                RESULT_VEHICLE_INFO_BLL bll = new RESULT_VEHICLE_INFO_BLL();
                var vInfo = bll.GetModelList("ID='" + strID + "'");
                if (vInfo.Length > 0)
                {
                    string strContent = "window.open('" + string.Format("PrintPZSQB.aspx?JCLSH={0}", vInfo[0].JCLSH) + "');";
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "uuu", strContent, true);
                }
            }
       
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {

           // PrintPZSQB();
            //return;

            string strJs = "";
            strJs += " var objPrint=document.getElementById('wfPrintOcx1');";
            //清除控件上的所有打印原件
            strJs += "objPrint.wfClear();";
            //授权名和授权码
            strJs += " objPrint.wfRegName = 'guofeng'; objPrint.wfRegCode = '0EBC2502A8059BCC2Q702014';";
            //设置打印纸张
            //支持Custom,A2,A3,A3Extra,A3Trans,A4,A4Extra,A4Plus,A4Small,A4Trans A5,A5Extra,A5Trans,B4,B4ISO,B5,B5Extra,B5Trans
            strJs += "objPrint.wfPaperSize='Custom';";
            //选择打印机，参数为打印机标识名
            //strJs += "objPrint.wfSelectPrinter='EPSON LQ-610KII ESC/P2';";
            //弹出选择打印机窗口
            //strJs += "objPrint.wfSelectPrinterDialog();"; 
            //设置打印方向，1为纵向、2为横向
            strJs += "objPrint.wfOrientation=1;";
            //设置打印纸宽度和高度(单位毫米)，此设置只有在纸张类型为Custom时才有效
            strJs += "objPrint.wfPaperWidth = 210;";
            strJs += "objPrint.wfPaperHeight = 297;";
            //设置打印纸的上下左右边距(单位毫米)
            strJs += "objPrint.wfPaperTopMargin = 0;";
            strJs += "objPrint.wfPaperBottomMargin = 0;";
            strJs += "objPrint.wfPaperLeftMargin = 0;";
            strJs += "objPrint.wfPaperRightMargin = 0;";
            //设置所有元件针对左边距的偏移量(单位毫米)
            strJs += "objPrint.wfOffsetLeft = 0;";
            //设置所有元件针对上边距的偏移量(单位毫米)
            strJs += "objPrint.wfOffsetTop = 18;";
            //添加一个打印元件，参数依次为：元件唯一标识，左边距(单位毫米)，上边距(单位毫米)，宽度(单位毫米)，高度(单位毫米)，字体名称，字体大小，字体眼色，字体背景颜色，字体样式，字体对齐方式
            //字体样式：0默认，1加粗，2加斜，3加下划线，4加删除线
            //字体对齐方式:0左对齐，1居中，2右对齐；注意：需要宽度为固定值
            //固定元件宽度，即文字将可以自动换行

            RESULT_VEHICLE_INFO info = new RESULT_VEHICLE_INFO();
            RESULT_VEHICLE_INFO_BLL bll = new RESULT_VEHICLE_INFO_BLL();

            string strJCLSH = txtJCLSH.Text.Trim();

            info = bll.GetModel(strJCLSH);


            strJs += "objPrint.wfAddLabel('SYR', 70, 28, 0, 0, '宋体', 12, '000000', '', 1, 1);";
            strJs += "objPrint.wfAddLabel('YZBH', 160, 28, 0, 0, '宋体', 12, '000000', '', 1,0);";
            strJs += "objPrint.wfAddLabel('ADDRESS', 70, 38, 0, 0, '宋体', 12, '000000', '', 1, 0);";
            strJs += "objPrint.wfAddLabel('LXDH', 70, 51, 0, 0, '宋体', 12, '000000', '', 1, 0);";
            strJs += "objPrint.wfAddLabel('DLRMC', 70, 62, 0, 0, '宋体', 12, '000000', '', 1, 0);";
            strJs += "objPrint.wfAddLabel('DLRDH', 155, 62, 0, 0, '宋体', 12, '000000', '', 1, 0);";
            strJs += "objPrint.wfAddLabel('HPZL', 52, 85, 0, 0, '宋体', 12, '000000', '', 1,0);";
            strJs += "objPrint.wfAddLabel('HPHM', 150, 85, 0, 0, '宋体', 12, '000000', '', 1,0);";

            strJs += "objPrint.wfAddLabel('XZ1', 28, 177, 0, 0, '宋体', 12, '000000', '', 1,0);";
            strJs += "objPrint.wfAddLabel('XZ2', 62, 177, 0, 0, '宋体', 12, '000000', '', 1,0);";

            strJs += "objPrint.wfAddLabel('YEAR', 143, 223, 0, 0, '宋体', 12, '000000', '', 1,0);";
            strJs += "objPrint.wfAddLabel('MONTH', 163, 223, 0, 0, '宋体', 12, '000000', '', 1,0);";
            strJs += "objPrint.wfAddLabel('DAY', 178, 223, 0, 0, '宋体', 12, '000000', '', 1,0);";
            
            //为组件赋值，参数为：元件唯一标识，元件要打印的文本
            strJs += "objPrint.wfAddValue('SYR', '" + info.SYR + "');";
            strJs += "objPrint.wfAddValue('YZBH', '" + info.YZBH + "');";
            strJs += "objPrint.wfAddValue('ADDRESS', '" + info.LXDZ + "');";
            strJs += "objPrint.wfAddValue('LXDH', '" + info.LXDH + "');";
            strJs += "objPrint.wfAddValue('DLRMC', '" + info.DLRMC + "');";
            strJs += "objPrint.wfAddValue('DLRDH', '" + info.DLRLXDH + "');";
            strJs += "objPrint.wfAddValue('HPZL', '" + info.HPZL + "');";
            strJs += "objPrint.wfAddValue('HPHM', '" + info.HPHM + "');";

            strJs += "objPrint.wfAddValue('XZ1', '√');";
            strJs += "objPrint.wfAddValue('XZ2', '√');";

            strJs += "objPrint.wfAddValue('YEAR', '" + DateTime.Now.Year + "');";
            strJs += "objPrint.wfAddValue('MONTH', '" + DateTime.Now.Month + "');";
            strJs += "objPrint.wfAddValue('DAY', '" + DateTime.Now.Day + "');";
            //立即打印到打印机
            strJs += "objPrint.wfPrint();";


            gfLibrary.gfAjaxJs.RunFunction(this.UpdatePanel1, strJs);
        }

        protected void btnPrintC_Click(object sender, EventArgs e)
        {
            try
            {
                string strJCLSH = txtJCLSH.Text.Trim();
                gfLibrary.gfAjaxJs.OpenNewUrl(this.UpdatePanel1, "Print_SQPZ.aspx?UniqueKey=" + strJCLSH);
            }
            catch (Exception ex)
            { 
            }
        }

        protected void btnEmpty_Click(object sender, EventArgs e)
        {
            try
            {
                gfLibrary.gfAjaxJs.OpenNewUrl(this.UpdatePanel1, "Print_SQPZ_Empty.aspx");
            }
            catch (Exception ex)
            {
            }
        }
    }
}